package cn.ffcs.uec.outStaff.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffVocquaEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffVocqua;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffVocquaEditComposer extends BasePortletComposer {

    private static final long serialVersionUID = -6096343177536382090L;

    private UecOutStaffVocquaEditBean bean = new UecOutStaffVocquaEditBean();

    private String opType;

    @Resource
    private IdentityCardConfigManager identityCardConfigManager;

    private UecOutStaffVocqua uecOutStaffVocqua;

    private UecOutStaffInfo uecOutStaffInfo;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
    }

    /**
     * window初始化.
     * 
     * @throws Exception
     *             异常
     */
    public void onCreate$uecOutStaffVocquaEditWin() throws Exception {
        bindCombobox();
        opType = (String) arg.get("opType");
        uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
        uecOutStaffVocqua = (UecOutStaffVocqua) arg.get("uecOutStaffVocqua");
        if (opType.equals("add")) {
            bean.getUecOutStaffVocquaEditWin().setTitle("外包人员职业资格新增");
        } else if (opType.equals("edit")) {
            bean.getUecOutStaffVocquaEditWin().setTitle("外包人员职业资格修改");
            strToDate();
        } else if (opType.equals("view")) {
            bean.getUecOutStaffVocquaEditWin().setTitle("外包人员职业资格查看");
            strToDate();
            PubUtil.disableComponent(bean.getUecOutStaffVocquaEditWin());
        }
    }

    /**
     * 保存.
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        String msg = checkUacStaffVocquaData();
        if (msg != null) {
            Messagebox.show(msg);
            return;
        }
        
        UecOutStaffVocqua uecOutStaffVocquaObj = UecOutStaffVocqua.newInstance();
        if (uecOutStaffVocqua != null) {
            PubUtil.fillPoFromEditorBean(uecOutStaffVocqua, bean);
        } else {
            uecOutStaffVocqua = uecOutStaffVocquaObj;
            uecOutStaffVocqua.setOutStaffId(uecOutStaffInfo.getOutStaffId());
            PubUtil.fillPoFromEditorBean(uecOutStaffVocqua, bean);
        }
        if (opType.equals("add")) {
            String strBeginDate = DateUtil.dateToStr(this.bean.getGetQualiDate().getValue());
            uecOutStaffVocqua.setGetQualiDate(strBeginDate);
            String strEndDate = DateUtil.dateToStr(this.bean.getQualiEffPeriod().getValue());
            uecOutStaffVocqua.setQualiEffPeriod(strEndDate);
            uecOutStaffVocqua.setOutStaffId(uecOutStaffVocqua.getOutStaffId());
            uecOutStaffVocqua.addOnly();
            Events.postEvent("onOK", this.self, uecOutStaffVocqua);
            onCancel();
            Messagebox.show("新增成功！");
        } else if (opType.equals("edit")) {
            String strBeginDate = DateUtil.dateToStr(this.bean.getGetQualiDate().getValue());
            uecOutStaffVocqua.setGetQualiDate(strBeginDate);
            String strEndDate = DateUtil.dateToStr(this.bean.getQualiEffPeriod().getValue());
            uecOutStaffVocqua.setQualiEffPeriod(strEndDate);
          
            uecOutStaffVocqua.updateOnly();
            Events.postEvent("onOK", this.self, uecOutStaffVocqua);
            onCancel();
            Messagebox.show("修改成功！");
        }
        onCancel();
    }

    /**
     * 取消.
     */
    public void onCancel() {
        bean.getUecOutStaffVocquaEditWin().onClose();
    }

    private String checkUacStaffVocquaData() {
        // BaseInfo Check
        /*
         * if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) { return
         * "请填写岗位名称"; } else if
         * (StrUtil.checkBlank(bean.getJobName().getValue())) { return
         * "岗位名称中有空格"; } else if
         * (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) { return
         * "请填写在本单位从事外包开始日期"; }
         */
        // IDCARD Real Name Check
        /*
         * if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
         * bean.getCertType().getSelectedItem().getValue())) { String idNum =
         * bean.getCertNumber().getValue(); String idName =
         * bean.getCertName().getValue(); try { boolean isRealName =
         * CertUtil.checkIdCard(idNum, idName);
         * 
         * if (!isRealName) { return "实名认证未通过"; } } catch (Exception e) { return
         * "调用国政通接口失败"; }
         * 
         * }
         */
        return null;
    }

    /**
     * 字符串转date
     */
    private void strToDate() {
        if (arg.get("uecOutStaffVocqua") != null) {
            uecOutStaffVocqua = (UecOutStaffVocqua) arg.get("uecOutStaffVocqua");
            PubUtil.fillBeanFromPo(uecOutStaffVocqua, bean);
            if (!StrUtil.isEmpty(uecOutStaffVocqua.getGetQualiDate())) {
                bean.getGetQualiDate().setValue(DateUtil.patternStrToDate(uecOutStaffVocqua.getGetQualiDate()));
            }
            if (!StrUtil.isEmpty(uecOutStaffVocqua.getQualiEffPeriod())) {
                bean.getQualiEffPeriod().setValue(DateUtil.patternStrToDate(uecOutStaffVocqua.getQualiEffPeriod()));
            }
        }
    }

    /**
     * 绑定下拉框 绑定combobox
     */
    private void bindCombobox() {
        // 是否小CEO
        List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
        String[][] isSend = { { "Y", "是" }, { "N", "否" } };
        for (int i = 0; i < isSend.length; i++) {
            NodeVo nodeVo = new NodeVo();
            for (int j = 0; j < 1; j++) {
                nodeVo.setId(isSend[i][j]);
                nodeVo.setName(isSend[i][j + 1]);
            }
            nodeVoIsSendList.add(nodeVo);
        }
        ListboxUtils.rendererForEdit(bean.getHighestLevelFlag(), nodeVoIsSendList);
        // 职业资格类别
        ListboxUtils.rendererForEdit(bean.getQualitype(), NodeVoUtil.getDictionaries(UecConstants.QUALITYPE));
        // 职业资格名称
        ListboxUtils.rendererForEdit(bean.getQualiname(), NodeVoUtil.getDictionaries(UecConstants.QUALINAME));
        // 职业资格等级
        ListboxUtils.rendererForEdit(bean.getQualilevel(), NodeVoUtil.getDictionaries(UecConstants.QUALILEVEL));
    }
}
